//
//  TKIMFileMsgView.m
//  TKIM
//
//  Created by liupm on 15/11/7.
//  Copyright © 2015年 liupm. All rights reserved.
//

#import "TKIMFileMsgView.h"
//#import "TKIMFileMessage.h"
//#import "TKIMFileHelper.h"
//#import "TKIMUIHelper.h"
#import "TKIMLayerUtils.h"

@interface TKIMFileMsgView()<UIDocumentInteractionControllerDelegate>

@property(nonatomic,strong) UIImageView *fileImageView;
@property(nonatomic,strong) TKIMFileMessage *fileMsg;
@property(nonatomic,strong) UIImageView *fileIcon;
@property(nonatomic,strong) UILabel *fileName;
@property(nonatomic,strong) UILabel *fileSzie;
@property(nonatomic,strong) UIDocumentInteractionController *controller;
@property(nonatomic,strong) TKIMLayerUtils *layerUtils;

@end

@implementation TKIMFileMsgView

/**
 *  @Author lpm, 15-07-30 11:07:12
 *
 *  @brief  组装界面
 */
-(void)drawView
{
    [super drawView];
    [self.backgroundImageView addSubview:self.fileImageView];
    [self.fileImageView addSubview:self.fileIcon];
    [self.fileImageView addSubview:self.fileName];
    [self.fileImageView addSubview:self.fileSzie];
}



-(UIImageView *)fileImageView
{
    if (!_fileImageView)
    {
        _fileImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, TKIMScreenWidth *0.6, 50)];
        _fileImageView.backgroundColor = [UIColor clearColor];
        _fileImageView.alpha = 0.8;
    }
    return _fileImageView;
}

-(UIImageView *)fileIcon
{
    if(!_fileIcon)
    {
        _fileIcon = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, 40, 40)];
        _fileIcon.image = TKIMGetImage(@"im_icon_file");
    }
    return _fileIcon;
}


-(UILabel *)fileName
{
    if (!_fileName)
    {
        _fileName = [[UILabel alloc] initWithFrame:CGRectMake(_fileIcon.right+5, _fileIcon.top, self.fileImageView.width - 50, 20)];
        _fileName.textColor = [UIColor grayColor];
        _fileName.font = [UIFont systemFontOfSize:15.0f];
        _fileName.backgroundColor = [UIColor clearColor];
    }
    return _fileName;
}


-(UILabel *)fileSzie
{
    if (!_fileSzie)
    {
        _fileSzie = [[UILabel alloc] initWithFrame:CGRectMake(self.fileName.left, self.fileName.top+self.fileName.height, self.fileName.width, self.fileName.height)];
        _fileSzie.textColor = [UIColor grayColor];
        _fileSzie.font = [UIFont systemFontOfSize:15.0f];
        _fileSzie.backgroundColor = [UIColor clearColor];
    }
    return _fileSzie;
}


/**
 *  @Author lpm, 15-07-27 17:07:14
 *
 *  @brief  设置文字
 *
 *  @param text
 */
-(void)setMessage:(TKIMMessage *)message
{
    [super setMessage:message];
    
    _fileMsg = (TKIMFileMessage *)message;
    //图片大小
    CGSize size = self.fileImageView.size;
    
    //设置消息label的大小
    CGRect msgRect = [self getMsgFrame:message size:size];
    self.fileName.text = _fileMsg.fileName;
    self.fileSzie.text = [TKIMFileHelper sizeformat:[_fileMsg.fileSize floatValue]];
    self.fileImageView.frame = msgRect;
    self.fileIcon.frame = CGRectMake(5, 5, 40, 40);
    self.fileName.frame =CGRectMake(_fileIcon.right+8, _fileIcon.top, self.fileImageView.width - 50, 20);
    self.fileSzie.frame = CGRectMake(self.fileName.left, self.fileName.top+self.fileName.height, self.fileName.width, self.fileName.height);
    
    if (!message.isSender)
    {
        self.fileSzie.textColor = [UIColor whiteColor];
        self.fileName.textColor = [UIColor whiteColor];
    }
    
    UITapGestureRecognizer *openFile = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(openFile:)];
    self.backgroundImageView.userInteractionEnabled = YES;
    [self.backgroundImageView addGestureRecognizer:openFile];
}


-(TKIMLayerUtils *)layerUtils
{
    if (!_layerUtils)
    {
        UIViewController *ctl = [TKIMUIHelper getCurrentVC];
        _layerUtils = [[TKIMLayerUtils alloc] initWithContent:ctl.view];
    }
    return _layerUtils;
}


/**
 *  @Author lpm, 15-10-26 17:10:01
 *
 *  @brief  打开文件
 *
 *  @param tap
 */
-(void)openFile:(UITapGestureRecognizer *)tap
{
    if(!self.fileMsg.isSender)
    {
        if (self.fileMsg.isDownloading)
        {
            return;
        }
        //如果本地没有文件则先下载
        if(![TKIMFileHelper fileIsExist:self.fileMsg.localUrl  fileSize:[self.fileMsg.fileSize longLongValue]])
        {
            [self.layerUtils showLoading:YES withLabel:@"文件下载中"];
            self.fileMsg.isDownloading = YES;
            [[TKIMManager shareInstance] fetchMessageFile:self.fileMsg complete:^(NSObject *obj) {
                [self.layerUtils showLoading:NO];
                [[TKIMLayerUtils instance] showTip:@"下载完成"];
                self.fileMsg.isDownloading = NO;
            } process:^(NSInteger totalBytes, NSInteger bytesWritten) {
                
            } fail:^(NSError *error) {
                [self.layerUtils showLoading:NO];
                [[TKIMLayerUtils instance] showTip:@"下载失败"];
            }];
        }
        else
        {
            NSURL *url = [NSURL fileURLWithPath:self.fileMsg.localUrl];
            [self open:url];
        }
    }
}


/**
 *  @Author lpm, 15-11-04 10:11:48
 *
 *  @brief  播放视频
 *
 *  @param url
 */
-(void)open:(NSURL *)url
{

    self.controller = [UIDocumentInteractionController interactionControllerWithURL:url];
    self.controller.delegate = self;
    [self.controller presentPreviewAnimated:YES];
}


- (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller
{
    return [TKIMUIHelper getCurrentVC];
}


/**
 *  @Author lpm, 15-11-05 09:11:46
 *
 *  @brief  行高
 *
 *  @return
 */
+(CGFloat)heightForRow:(TKIMMessage *)msg
{
    //图片大小
    CGSize size = CGRectMake(0, 0, TKIMScreenWidth *0.3, 50).size;
    return   size.height + TKIMChatMsgPadding+10;
}


@end
